package com.wing.product.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.response.WarehouseProductionSchedulingPlanDetailVO;
import com.wing.product.service.WarehouseProductionSchedulingPlanDetailService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api(value = "生产排产计划明细管理", tags = "#/warehouse/product/warehouseProductionSchedulingPlanDetail")
@RestController("WarehouseProductionSchedulingPlanDetailController")
@RequestMapping("/warehouse/productionSchedulingPlanDetail")
@RequiredArgsConstructor
public class WarehouseProductionSchedulingPlanDetailController {

    private final WarehouseProductionSchedulingPlanDetailService warehouseProductionSchedulingPlanDetailService;

    @ApiOperation(value = "查询所有", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseProductionSchedulingPlanDetailVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "999", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order,
            Long productionPlanId,
            String productionPlanCode,
            String code,
            Long customerId,
            String status,
            String customerName,
            String userBundleCode
    ){
        if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)){
            customerId = JwtUtils.getDepartmentId();
        }
        Page<WarehouseProductionSchedulingPlanDetailVO> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", order);
        paramMap.put("productionPlanId", productionPlanId);
        paramMap.put("productionPlanCode", productionPlanCode);
        paramMap.put("customerName", customerName);
        paramMap.put("userBundleCode", userBundleCode);
        paramMap.put("code", code);
        paramMap.put("customerId", customerId);
        paramMap.put("status", status);

        //搜索
        IPage<WarehouseProductionSchedulingPlanDetailVO> list = warehouseProductionSchedulingPlanDetailService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "修改状态", notes = "修改")
    @GetMapping ("/updateProductionSchedulingPlanDetail")
    public JsonResult updateProductionSchedulingPlanDetail(@RequestParam String id, @RequestParam String status){
        if (!StringUtils.isEmpty(id)){
            return warehouseProductionSchedulingPlanDetailService.updateProductionSchedulingPlanDetail(id,status);
        }else {
            return  JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "生产看板制造令", notes = "搜索")
    @GetMapping("/selectProductionSchedulingPlan")
    public JsonResult<Map<Object, Object>> selectProductionSchedulingPlan() {
        return warehouseProductionSchedulingPlanDetailService.selectProductionSchedulingPlan();
    }
}
